import { RootContext } from '@/App.context'
import { makeAutoObservable } from 'mobx'

export const ROLES = ['system', 'user', 'assistant'] as const
export type MessageRole = typeof ROLES[number]

export type RequestMessage = {
  role: MessageRole
  content: string
}

// 话题
export interface ChatSession {
  id: string
  topic: string
  memoryPrompt: string
  messages: ChatMessage[]
  lastUpdate: number
}

// 消息
export type ChatMessage = RequestMessage & {
  id: string
  time: number
  streaming?: boolean
  isError?: boolean
}

export default class ChatStore {
  public context: RootContext

  public assistantAvatar =
    // 'https://s3.bmp.ovh/imgs/2023/08/09/5a49f5d6eff974cc.jpg'
    'https://s3.bmp.ovh/imgs/2023/08/19/11b3b1042e7af14b.png'
  public currentSession: ChatSession = {
    id: 'testSession',
    topic: 'test',
    memoryPrompt: 'abc',
    messages: [
      {
        id: 'message_1',
        time: Math.floor(Date.now() / 1000),
        role: ROLES[2],
        content:
          '我是一名AI机器人，基于GPT-3.5开发。我具备语言理解和生成的能力，可以回答各种问题，提供实用信息和解决方案。\n',
      },
      // {
      //   id: 'message_2',
      //   time: 1645255880,
      //   role: ROLES[1],
      //   content: '用户消息内容',
      // },
      // {
      //   id: 'message_3',
      //   time: 1645255890,
      //   role: ROLES[2],
      //   content:
      //     '```json\n' +
      //     '{\n' +
      //     '  "error": true,\n' +
      //     '  "message": "NetworkError when attempting to fetch resource."\n' +
      //     '}\n' +
      //     '```',
      //   streaming: true,
      // },
    ],
    lastUpdate: 111111,
  }
  public history: { type: 'human' | 'ai'; content: string }[] = [
    {
      type: 'human',
      content: '你好！',
    },
    {
      type: 'ai',
      content: '你好，我是你的小助手，可以问我任何问题。',
    },
  ]

  public typing = false

  public graphData = { nodes_get: [], links_get: [] }

  public recommendNode = { funcNodes: [], sustainNodes: [], techNodes: [] }

  public userInput = ''

  public reStart = false

  constructor(context: RootContext) {
    this.context = context
    makeAutoObservable(this, {}, { autoBind: true })
  }

  setTyping = (status: boolean) => {
    this.typing = status
  }
  addHistory = (type: 'human' | 'ai', content: string) => {
    this.history = [...this.history]
    this.history.push({ type: type, content: content })
  }
  popHistory = () => {
    this.history = [...this.history]
    this.history.pop()
  }
  replaceContent = (content: string) => {
    this.history = [...this.history]
    this.history[this.history.length - 1].content = content
  }
  setGraphData = (data) => {
    this.graphData = data
  }
  setRecommendNode = (data) => {
    this.recommendNode = data
  }
  setUserInput = (data: string) => {
    this.userInput = data
  }
  setReStart = (status: boolean) => {
    this.reStart = status
  }

  setMessageValue = (
    messageIndex: string,
    value: { time?: number; streaming?: boolean; content?: string }
  ) => {
    for (let i = 0; i < this.currentSession.messages.length; i++) {
      if (this.currentSession.messages[i]['id'] === messageIndex) {
        this.currentSession.messages[i] = {
          ...this.currentSession.messages[i],
          ...value,
        }
        break
      }
    }
  }

  addUserMessage = (content: string) => {
    const newMsg: ChatMessage = {
      id: `message_${this.currentSession.messages.length + 1}`,
      time: Math.floor(Date.now() / 1000),
      role: ROLES[1],
      content: content,
      streaming: true, // 临时内容
    }
    this.currentSession.messages.push(newMsg)
  }
}
